<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="author" content="hexinyang">
	<title>换卡</title>
	<script language="javascript" type="text/javascript" src="../script/print.js"></script>
	<script language="javascript" src="../script/common.js" type="text/javascript"></script>	
	<script language="javascript" src="../script/card.js" type="text/javascript"></script>
<script type="text/javascript">
	var oldCardType;
	
	//页面加载方法	
	function load(){			
		Eui.showNavigation();
		setPages(["detview","readcardview"]);
		CardMgnt.cardUser.show("userinfo_div");
	}
	//查询显示信息明细
	function readOldCard(){
		var asn;
		try{
			getCard();
			asn = getAsn();
		}catch(e){showError(e.description);return;}		
		cardForm.oldAsn.value = asn;
		
		DWR.call("mgntQueryService.checkResellCard",asn,function(data){
			oldCardType = data.cardType;
			
			var card = getCardDetail({disBalance:true,disLoyalty:true,grayStatus:true,tradeDate:data.serverTime})
			if (card.grayFund || card.grayPoint){
				showWarning("灰锁状态的卡片不能执行此操作。");return;
			}
			var ct = data.cardType;
			cardForm.oldCardType.value = ct.id
			cardForm.oldCardTypeName.value = ct.name;
			cardForm.oldForegift.value = Format.fenToYuan(data.card.foregiftAmount);
			cardForm.feeAmount.value = Format.fenToYuan(CardMgnt.getFee(Constants.ACCOUNT_FEE_CHANGE_CARD,ct.id));
			CardMgnt.cardUser.showData("userinfo_div",data);
			
			var bObj = {}
			bObj.spareBalance = Format.fenToYuan(data.balanceMap?data.balanceMap.balance:0);
			bObj.spareLoyaltyBalance = Format.fenToYuan(data.balanceMap?data.balanceMap.loyaltyBalance:0);
			bObj.cardBalance = Format.fenToYuan(card.cardBalance);
			bObj.cardLoyaltyBalance = Format.fenToYuan(card.cardLoyaltyBalance);
			bObj.cardPin = card.cardPin;
			Common.setValues(balanceForm,bObj);
			
			if (card.cardBalance > 0 || card.cardLoyaltyBalance > 0){
				$("unloadCardBtn").disabled = false;
			}else{
				DWR.syncCall("cardTxService.revocateCardConsistent",asn,[AccountType.FUND,AccountType.POINT]);
				$("readNewCardBtn").disabled = false;
			} 
			$("queryOldCardBtn").disabled = true;
		});
	}
	
	function unloadCard(){
		if (showQuestion("换卡需要圈提卡内所有资金、积分，是否圈提？")){
			var obj = Common.getValues(balanceForm);
			try{
				CardMgnt.cardUnload(cardForm.oldAsn.value,Format.yuanToFen(obj.cardBalance),Format.yuanToFen(obj.cardLoyaltyBalance),obj.cardPin);
			}catch(e){
				showError(e.description);
				return;
			}
			var bObj = Common.getValues(balanceForm);
			bObj.spareBalance = new Number(bObj.spareBalance) + new Number(bObj.cardBalance);
			bObj.spareLoyaltyBalance = new Number(bObj.spareLoyaltyBalance) + new Number(bObj.cardLoyaltyBalance);
			bObj.cardBalance = 0;
			bObj.cardLoyaltyBalance = 0;
			Common.setValues(balanceForm,bObj);
			
			$("readNewCardBtn").disabled = false;
			$("unloadCardBtn").disabled = true;
		}else
			onCancel();
	}
	
	//重置方法
	function onCancel(){
		switchPage("readcardview");
		cardForm.reset();
		balanceForm.reset();
		CardUserViewForm.reset();
		$("unloadCardBtn").disabled = true;
		$("changeCardBtn").disabled = true;
		$("readNewCardBtn").disabled = true;
		$("queryOldCardBtn").disabled = false;
	}
	
	//读新卡
	function readNewCard(){
		var asn;
		try{
			getCard();
			asn = getAsn();
		}catch(e){showError(e.description);return;}
		
		cardForm.newAsn.value = asn;
		
		DWR.call("mgntQueryService.checkInitedCard",asn,function(data){
			var currentLevel = $(CardMgnt.cardUser.formName).currentLevel.value;
			var cardType = data.cardType;
			if (cardType.baseCardType != oldCardType.baseCardType || oldCardType.anonymous !== cardType.anonymous){
				showWarning("新卡类型不匹配,不能领取该卡！");return;
			}
			if (currentLevel < data.cardType.userLevel){
				showWarning("该客户级别不能领取该类型("+cardType.name+")卡！");return;
			}
			
			var foregiftAmount = CardMgnt.getFee(Constants.ACCOUNT_FOREGIFT_CARD,data.cardType.id);
			cardForm.newForegift.value = Format.fenToYuan(foregiftAmount);
			cardForm.newCardTypeName.value = data.cardType.name;
			$("changeCardBtn").disabled = false;
		});
	}
	
	
	//换卡方法
	function changeCard(){
		var oldAsn = cardForm.oldAsn.value;
		var newAsn = cardForm.newAsn.value;
		if(!CardMgnt.checkCardAsn(newAsn))return;	
		if (!showQuestion("是否换卡？")) return;
		DWR.call("cardMgntService.resellCard",oldAsn,newAsn,function(data){
			var str = "换卡成功！<br>新卡卡号为："+newAsn+"<br>备用金余额为："+Format.fenToYuan(data.balance)+"元";
			str += "<br>备用积分余额为：" + Format.fenToYuan(data.pointBalance) + "点";
			showInformation(str);
			
			switchPage("detview");
			CardMgnt.showPrintView("detview","blockCard_bill",data.logId,"换卡凭证");
			
			try{
				WebPrint.print("billview");
			}catch(e){
				showError("打印单据出错:"+e.description);
			}
			while(true){
				try{
					CardMgnt.syncCardData(newAsn);
					break;
				}catch(e){
					if (showQuestion("同步卡数据失败！是否重试？<br>错误："+e.description)) continue;
					
					if(showQuestion("同步卡数据失败！是否换卡？")){
						CardMgnt.badCard.gotoPage(newAsn,CardMgnt.badCard.CHANGECARD);
					}
					return;
				}
			}
			if (data.cardUser.usePin){
				CardMgnt.startCardPin(newAsn);
			}
		},{blockScreen:true});
	}
</script>
</head>
<body onload="load()">
	<!--页面导航-->
	<div class="nav"><div id="nav">&nbsp;</div></div>
	<div id="readcardview">
		<div class="form">
			<div id="userinfo_div">
			</div>
			<form id="balanceForm" name="balanceForm">
				<input type="hidden" name="cardPin">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>备用金余额</label> <input type="text" name="spareBalance" readonly class="readonly"> 元</td>
						<td><label>备用积分余额</label> <input type="text" name="spareLoyaltyBalance" readonly class="readonly">&nbsp;&nbsp;</td>
					</tr>
					<tr>
						<td><label>旧卡资金余额</label> <input type="text" name="cardBalance" readonly class="readonly"> 元</td>
						<td><label>旧卡积分余额</label> <input type="text" name="cardLoyaltyBalance" readonly class="readonly">&nbsp;&nbsp;</td>
					</tr>
				</table>
			</form>
		</div>
		<div class="form">
			<form id="cardForm" name="cardForm">
				<input type="hidden" name="oldCardType">
				<div class="formTitle">卡片信息</div>
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>旧卡卡号</label> <input type="text" name="oldAsn" readonly class="readonly"><span class="des">*</span></td>
						<td><label>新卡卡号</label> <input type="text" name="newAsn" readonly class="readonly">&nbsp;&nbsp;</td>
					</tr>
				</table>
				<input type="hidden" name="newCardType">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>旧卡卡类型</label> <input type="text" name="oldCardTypeName" readonly class="readonly"></td>
						<td><label>新卡卡类型</label> <input type="text" name="newCardTypeName" readonly class="readonly"></td>
					</tr>
				</table>
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>旧卡押金</label> <input type="text" name="oldForegift" readonly class="readonly"></td>
						<td><label>新卡押金</label> <input type="text" name="newForegift" readonly class="readonly"></td>
					</tr>
					<tr>
						<td><label>手续费</label> <input type="text" name="feeAmount" readonly class="readonly">&nbsp;&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
				</table>
			</form>
		</div>
		<p>
			<button id="queryOldCardBtn" onclick="readOldCard()">读 旧 卡</button>
			<button id="unloadCardBtn" onclick="unloadCard()" disabled>圈提资金</button>
			<button id="readNewCardBtn" onclick="readNewCard()" disabled>读 新 卡</button>
			<button id="changeCardBtn" onclick="changeCard()" disabled>换 卡</button>
			<button onclick="onCancel()">重 置</button>
		</p>
	</div>
	
	<!--凭证页面-->
	<div id="detview" style="display:none;">
	</div>
</body>
</html>